<?php
// Send an empty HTTP 200 OK response to acknowledge receipt of the notification
header('HTTP/1.1 200 OK');

// Assign payment notification values to local variables. There are more variables, however, these are the most essential ones. If you passed more variables through your form see https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/ for a full list of variables
$payer_email = $_POST['payer_email'];
$address_country = $_POST['address_country'];
$address_city = $_POST['address_city'];
$address_name = $_POST['address_name'];
$address_state = $_POST['address_state'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payer_business_name = $_POST['payer_business_name'];
$memo = $_POST['memo'];
$item_name = $_POST['item_name1'];
$quantity = $_POST['quantity1'];
$payment_amount = $_POST['mc_gross_1'];
$name = $first_name ." ". $last_name ." ". $payer_business_name;
$address = $address_name ."<br>". $address_street ."<br>". $address_city ." ". $address_state ." ". $address_zip ."<br>". $address_country;

// Mail variables
$to = 'email@example.com';
// Need to set timezone for date() to work
date_default_timezone_set('America/New_York');
$subject = 'Order placed on '.date("F d, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<center><h1>An order has been placed.</h1></center>';
$message .= '<p>Customer email:<br>'.$payer_email.'</p>';
$message .= '<p>Customer name:<br>'.$name.'</p>';
$message .= '<p>Customer address:<br>'.$address.'</p>';
if (!empty($memo)) { $message .= '<p>Message from customer:<br>'.$memo.'</p>';}
$message .= '<p>Item placed:<br>'.$item_name.'</p>';
$message .= '<p>Item quantity:<br>'.$quantity.'</p>';
$message .= '<p>Payment amount:<br>$'.$payment_amount.'</p>';
$message .= '</body></html>';


// Build the required acknowledgement message out of the notification just received
$req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
  $value = urlencode(stripslashes($value));  // Encode these values
  $req  .= "&$key=$value";                   // Add the NV pairs to the acknowledgement
}


// Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

// Open a socket for the acknowledgement request
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// Send the HTTP POST request back to PayPal for validation
fputs($fp, $header . $req);

while (!feof($fp)) {                     // While not EOF
  $res = fgets($fp, 1024);               // Get the acknowledgement response
  if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification

    // IPN is verified
    // Code here
    mail($to, $subject, $message, $headers);

  }
  else if (strcmp ($res, "INVALID") == 0) { //Response contains INVALID - reject notification

    // Something went wrong -- begin error handling
    // Code here
    mail($to, "Invalid IPN", "Invalid IPN", $headers);

  }
}
fclose($fp);  // Close the file
?>
